﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.MultiEditingInPathEditor.SetLocationsProperties.CApSLPSetLocationsPropertiesLogic
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.Olp.RoboticParameters;


namespace DnProcessSimulateCommands.MultiEditingInPathEditor.SetLocationsProperties;

public class CApSLPSetLocationsPropertiesLogic
{
  private const double _EPSILON = 1E-06;
  private const string _PARAM_GUN_STATE = "SW_GUN_STATE";
  private const string _PARAM_GUN_WAIT = "SW_GUN_WAIT";
  private const string _PARAM_ZONE = "RRS_ZONE_NAME";
  private const string _PARAM_WELD_TIME = "SW_TIME_ON_PT";
  private const string _PARAM_COOL_DOWN_TIME = "SW_WAIT_TIME";
  private const string _PARAM_TIME = "RRS_MOTION_TIME";
  private const string _PARAM_PROCESS_TYPE = "ProcessType";

  public static TxObjectList GetSelectedLocations()
  {
    bool flag = CApSLPSetLocationsPropertiesEnabler.ShouldCheckLocationForCanBeModified();
    TxObjectList selectedLocations = new TxObjectList();
    TxTypeFilter txTypeFilter = new TxTypeFilter();
    txTypeFilter.AddIncludedType(typeof (ITxOrderedCompoundOperation));
    txTypeFilter.AddIncludedType(typeof (ITxRoboticLocationOperation));
    txTypeFilter.AddIncludedType(typeof (ITxCompoundOperation));
    foreach (ITxObject filteredItem in (Collection<ITxObject>) TxApplication.ActiveSelection.GetFilteredItems((ITxTypeFilter) txTypeFilter))
    {
      TxObjectList txObjectList = (TxObjectList) null;
      switch (filteredItem)
      {
        case ITxRoboticLocationOperation _:
          txObjectList = new TxObjectList();
          ((Collection<ITxObject>) txObjectList).Add(filteredItem);
          break;
        case ITxOrderedCompoundOperation _:
          txObjectList = ((ITxObjectCollection) (filteredItem as ITxOrderedCompoundOperation)).GetAllDescendants((ITxTypeFilter) new TxTypeFilter(typeof (ITxRoboticLocationOperation)));
          break;
        case ITxCompoundOperation _:
          txObjectList = ((ITxObjectCollection) (filteredItem as ITxCompoundOperation)).GetAllDescendants((ITxTypeFilter) new TxTypeFilter(typeof (ITxRoboticLocationOperation)));
          break;
      }
      if (txObjectList != null)
      {
        foreach (ITxRoboticLocationOperation locationOperation in (Collection<ITxObject>) txObjectList)
        {
          if (!((Collection<ITxObject>) selectedLocations).Contains((ITxObject) locationOperation) && (!flag || ((ITxObject) locationOperation).CanBeModified))
            ((Collection<ITxObject>) selectedLocations).Add((ITxObject) locationOperation);
        }
      }
    }
    return selectedLocations;
  }

  public static bool DoAllSelectedLocationsHaveSameController()
  {
    ITxRobotController commonController = (ITxRobotController) null;
    return CApSLPSetLocationsPropertiesLogic.GetCommonController(out commonController);
  }

  public static TxRobot GetRobot()
  {
    TxRobot robot = (TxRobot) null;
    TxObjectList selectedLocations = CApSLPSetLocationsPropertiesLogic.GetSelectedLocations();
    if (((Collection<ITxObject>) selectedLocations).Count > 0 && ((Collection<ITxObject>) selectedLocations)[0] is ITxRoboticLocationOperation)
      robot = (((Collection<ITxObject>) selectedLocations)[0] as ITxRoboticLocationOperation).ParentRoboticOperation.Robot as TxRobot;
    return robot;
  }

  public static bool GetCommonController(out ITxRobotController commonController)
  {
    bool commonController1 = true;
    commonController = (ITxRobotController) null;
    TxObjectList selectedLocations = CApSLPSetLocationsPropertiesLogic.GetSelectedLocations();
    bool flag = false;
    foreach (ITxRoboticLocationOperation locationOperation in (Collection<ITxObject>) selectedLocations)
    {
      if (((ITxObject) locationOperation).Collection != TxApplication.ActiveDocument.OperationRoot && locationOperation.ParentRoboticOperation != null)
      {
        if (locationOperation.ParentRoboticOperation.Robot != null)
        {
          if (!flag && commonController == null)
          {
            commonController = (ITxRobotController) locationOperation.ParentRoboticOperation.Robot.Controller;
            flag = true;
          }
          else if (locationOperation.ParentRoboticOperation.Robot.Controller != null && commonController != null)
          {
            if (commonController.Name != locationOperation.ParentRoboticOperation.Robot.Controller.Name)
            {
              commonController1 = false;
              commonController = (ITxRobotController) null;
              break;
            }
          }
          else if (commonController is TxRobotRRSController && locationOperation.ParentRoboticOperation.Robot.Controller is TxRobotRRSController && (commonController as TxRobotRRSController).Version != (locationOperation.ParentRoboticOperation.Robot.Controller as TxRobotRRSController).Version)
          {
            commonController1 = false;
            commonController = (ITxRobotController) null;
            break;
          }
        }
        else
        {
          commonController1 = false;
          commonController = (ITxRobotController) null;
          flag = true;
        }
      }
    }
    return commonController1;
  }

  public static ITxRobotController GetController(ITxRoboticLocationOperation location)
  {
    try
    {
      return location != null && location.ParentRoboticOperation != null && location.ParentRoboticOperation.Robot != null ? (ITxRobotController) location.ParentRoboticOperation.Robot.Controller : (ITxRobotController) null;
    }
    catch (Exception ex)
    {
      return (ITxRobotController) null;
    }
  }

  public static bool IsPropertyValidOnLocation(
    string propertyName,
    ITxRoboticLocationOperation location)
  {
    bool flag = true;
    switch (propertyName)
    {
      case "SW_GUN_STATE":
        flag = location is TxWeldLocationOperation || location is TxRoboticViaLocationOperation;
        break;
      case "SW_GUN_WAIT":
        flag = location is TxWeldLocationOperation || location is TxRoboticViaLocationOperation;
        break;
      case "RRS_ZONE_NAME":
        flag = location is TxRoboticViaLocationOperation || location is TxRoboticSeamLocationOperation;
        break;
      case "SW_TIME_ON_PT":
        flag = location is TxWeldLocationOperation;
        break;
      case "SW_WAIT_TIME":
        flag = location is TxWeldLocationOperation;
        break;
      case "RRS_MOTION_TIME":
        flag = location is TxWeldLocationOperation || location is TxRoboticViaLocationOperation;
        break;
    }
    return flag;
  }

  public static TxOlpRoboticParametersManager GetRoboticManager(TxObjectList locations)
  {
    string str = string.Empty;
    if (locations != null && ((Collection<ITxObject>) locations).Count > 0 && ((Collection<ITxObject>) locations)[0] is ITxRoboticLocationOperation)
    {
      ITxRobotController controller = CApSLPSetLocationsPropertiesLogic.GetController(((Collection<ITxObject>) locations)[0] as ITxRoboticLocationOperation);
      if (controller != null)
        str = controller.Name;
    }
    TxOlpRoboticParametersManager roboticManager = new TxOlpRoboticParametersManager(str);
    roboticManager.SetManager();
    return roboticManager;
  }

  public static string GetListAsString(List<string> list, string separator)
  {
    string listAsString = string.Empty;
    if (list != null && list.Count > 0 && separator != null)
    {
      foreach (string str in list)
        listAsString = !(str == "") ? listAsString + str + separator : $"{listAsString} {separator}";
      listAsString = listAsString.Remove(listAsString.LastIndexOf(separator), separator.Length);
    }
    return listAsString;
  }

  public static List<string> UniteLists(List<string> listA, List<string> listB)
  {
    List<string> stringList = new List<string>((IEnumerable<string>) listA);
    if (listA == null && listB == null)
      return (List<string>) null;
    if (listB != null)
    {
      foreach (string str in listB)
      {
        if (!stringList.Contains(str))
          stringList.Add(str);
      }
    }
    return stringList;
  }

  public static List<string> IntersectLists(List<string> listA, List<string> listB)
  {
    List<string> stringList = new List<string>();
    foreach (string str in listA)
    {
      if (listB.Contains(str))
        stringList.Add(str);
    }
    return stringList;
  }

  public static void GetRowFormat(
    string propertyId,
    object value,
    TxOlpUnitType unitType,
    out string displayFormat,
    out object fixedValue)
  {
    displayFormat = string.Empty;
    fixedValue = (object) null;
    if (value == null)
      return;
    switch ((int) unitType)
    {
      case 1:
        displayFormat = "N" + TxApplication.Options.Units.LinearDigitsAfterDecimalPoint.ToString();
        double result1;
        if (double.TryParse(value.ToString(), out result1))
        {
          fixedValue = (object) (result1 / TxApplication.Options.Units.LinearMultiplier);
          break;
        }
        fixedValue = value;
        break;
      case 2:
        displayFormat = "N" + TxApplication.Options.Units.AngularDigitsAfterDecimalPoint.ToString();
        double result2;
        if (double.TryParse(value.ToString(), out result2))
        {
          fixedValue = (object) (result2 / TxApplication.Options.Units.AngularMultiplier);
          break;
        }
        fixedValue = value;
        break;
      case 3:
        displayFormat = "N" + TxApplication.Options.Units.TimeDigitsAfterDecimalPoint.ToString();
        double result3;
        if (double.TryParse(value.ToString(), out result3))
        {
          fixedValue = (object) (result3 / TxApplication.Options.Units.TimeMultiplier);
          break;
        }
        fixedValue = value;
        break;
      case 4:
        displayFormat = "N" + TxApplication.Options.Units.MassDigitsAfterDecimalPoint.ToString();
        double result4;
        if (!double.TryParse(value.ToString(), out result4))
          break;
        fixedValue = (object) (result4 / TxApplication.Options.Units.MassMultiplier);
        break;
      case 5:
      case 6:
        displayFormat = "N0";
        int result5;
        if (int.TryParse(value.ToString(), out result5))
        {
          displayFormat = "N2";
          fixedValue = (object) result5;
          break;
        }
        double result6;
        if (double.TryParse(value.ToString(), out result6))
        {
          fixedValue = (object) result6;
          break;
        }
        fixedValue = value;
        break;
      default:
        displayFormat = string.Empty;
        fixedValue = value;
        break;
    }
  }

  public static object GetBasicFormat(string propertyId, object value, TxOlpUnitType unitType)
  {
    object basicFormat = (object) null;
    if (value != null)
    {
      switch ((int) unitType)
      {
        case 1:
          double result1;
          basicFormat = !double.TryParse(value.ToString(), out result1) ? value : (object) (result1 * TxApplication.Options.Units.LinearMultiplier);
          break;
        case 2:
          double result2;
          basicFormat = !double.TryParse(value.ToString(), out result2) ? value : (object) (result2 * TxApplication.Options.Units.AngularMultiplier);
          break;
        case 3:
          double result3;
          basicFormat = !double.TryParse(value.ToString(), out result3) ? value : (object) (result3 * TxApplication.Options.Units.TimeMultiplier);
          break;
        case 4:
          double result4;
          if (double.TryParse(value.ToString(), out result4))
          {
            basicFormat = (object) (result4 * TxApplication.Options.Units.MassMultiplier);
            break;
          }
          break;
        case 5:
        case 6:
          int result5;
          double result6;
          basicFormat = !int.TryParse(value.ToString(), out result5) ? (!double.TryParse(value.ToString(), out result6) ? value : (object) result6) : (object) result5;
          break;
        default:
          basicFormat = value;
          break;
      }
    }
    return basicFormat;
  }

  public static bool AreEqual(object obj1, object obj2)
  {
    return obj1 == null && obj2 == null || obj1 != null && obj2 != null && (obj1 is double && obj2 is double num && Math.Abs((double) obj1 - num) <= 1E-06 || obj1.Equals(obj2));
  }

  public static bool AreAllLocationsOfTheSameProcessType(TxObjectList locations)
  {
    bool flag = false;
    string str = (string) null;
    if (locations != null)
    {
      foreach (ITxRoboticOperation location in (Collection<ITxObject>) locations)
      {
        TxRoboticParam parameter = location.GetParameter("ProcessType");
        if (parameter != null && parameter is TxRoboticStringParam)
        {
          if (str == null)
          {
            str = (parameter as TxRoboticStringParam).Value;
            flag = true;
          }
          else if (str == (parameter as TxRoboticStringParam).Value)
          {
            flag = true;
          }
          else
          {
            flag = false;
            break;
          }
        }
        else
          flag = false;
      }
    }
    return flag;
  }
}
